; Hayes V-series 2400 quadplus modem driver         Version 1.04 21-Apr-1991
; (c) 1990/1991 Hugo Fiennes

modem_initialise ()
  {
  integer oldtx=port_txspeed(),oldrx=port_rxspeed()

  port_rxclear()
  port_txspeed(9600)
  port_rxspeed(9600)

  type "AT"+cr
  pause(50)
  port_rxclear()

  type "ATX4E0Q0V1&C1&D2S0=0S9=1S10=5"+cr
  pause(50)
  type "ATB0N2S2=255&K1&Q0S37=6W2"+cr

  if (waitfor("OK",150))
    {
    port_txspeed(oldtx)
    port_rxspeed(oldrx)
    return(0)
    }

  port_txspeed(oldtx)
  port_rxspeed(oldrx)
  return(1)
  }

modem_shutdown ()
  {
  return(1)
  }

modem_connect ()
  {
  port_dtr(1)
  port_rts(1)
  type "ATO"+cr

  return(_processmessage(""))
  }

modem_disconnect ()
  {
  port_rts(0)
  port_dtr(0)
  pause 50
  port_dtr(1)
  port_rts(1)
  type cr
  }

modem_dial (string number[40],integer how)
  {
  string dialtype[1]

  port_dtr(1)
  port_rts(1)
  port_rxclear()

  if (how==0)
    {
    dialtype="P"
    }
  else
    {
    dialtype="T"
    }

  type "ATD"+dialtype+number+cr

  return(_processmessage(""))
  }

modem_errorcontrol (string option[10])
  {
  set(linklevel,none)

  if (comparei(option,"off"))
    {
    type("AT&Q0"+cr)
    }

  if (comparei($left(option,3),"mnp"))
    {
    type("AT&Q5"+cr)
    }

  if (comparei(option,"vasscom"))
    {
    type("AT&Q0"+cr)
    set(linklevel,vasscom)
    }

  waitfor("OK"+$chr(13)+$chr(10),100)
  pause(50)
  }

modem_answer ()
  {  
  string response[30]

  type "ATS0=1"+cr

  repeat
    {
    response=$modeminput(30,modem_replywait)
    }
  until(strcmp($left(response,7),"CONNECT")==0)

  return(_processmessage(response))
  }

modem_standard (string option[10])
  {
  if (comparei(option,"v21"))
    {
    port_txspeed(300)
    port_rxspeed(300)
    return
    }
  if (comparei(option,"v22"))
    {
    port_txspeed(1200)
    port_rxspeed(1200)
    return
    }
  if (comparei(option,"v22bis"))
    {
    port_txspeed(2400)
    port_rxspeed(2400)
    return
    }
  if (comparei(option,"v27"))
    {
    port_txspeed(4800)
    port_rxspeed(4800)
    return
    }
  if (comparei(option,"v32"))
    {
    port_txspeed(9600)
    port_rxspeed(9600)
    return
    }
  if (comparei(option,"v32bis"))
    {
    port_txspeed(19200)
    port_rxspeed(19200)
    return
    }
  prints "Modem standard "+option+" is not supported."+newline
  }

_processmessage (string already[30])
  {
  string retcode[30]

  if (len(already)==0)
    {
    ; Wait for message
    retcode=$modeminput(30,modem_replywait)

    ; Check for AT as first string as there is a bug with V23 (it echos
    ; AT even if echo is off at 1200/75)
    if (len(retcode)==0 || compare(retcode,"AT")!=0)
      {
      retcode=$modeminput(30,modem_replywait)
      }
    }
  else
    {
    retcode=already
    }

  ; Check for NO DIALTONE/BUSY
  switch(retcode)
    {
    case$("BUSY")
      {
      return(2)
      }
    case$("NO DIALTONE")
      {
      return(3)
      }
    }

  if (compare(retcode,"CONNECT"))
    {
    retcode="CONNECT 300"
    }
  if (compare($left(retcode,7),"CONNECT"))
    {
    integer baudrate,tx,rx,a=9
    string rate[5]

    ; Strip rate
    while(isdigit($mid(retcode,a)))
      {
      rate=rate+$mid(retcode,a)
      a=a+1
      }

    baudrate=val(rate)
    if (baudrate==1275)
      {
      tx=75
      rx=1200
      }
    else
      {
      tx=baudrate
      rx=baudrate
      }
    port_txspeed(tx)
    port_rxspeed(rx)
    prints retcode+newline
    return(0)
    }

  modem_disconnect()
  return(1)
  }
